<?php

namespace app\shopapi\behavior;

//
use app\shopapi\model\Shop;
use app\shopapi\model\ShopBillsStat;
use app\shopapi\model\lakala\ShopInfeed;

class ShopBehavior
{
    public function run()
    {

        //创建订单统计
        ShopBillsStat::add();
        //查询商户申请进件状态
        $shop_list = Shop::alias('s')
            //->join('user u','s.user_id = u.id')
            ->where('s.audit_status','not in','3,4')
            ->where('s.out_member_no','<>','')
            ->field('s.out_member_no,s.shop_id')
            ->select();
        $auth_model = new ShopInfeed();

        foreach ($shop_list as $k=>$v){
            $list = $auth_model->memberMerchantInfo($v['shop_id']);

            if ($list){
                foreach ($list['merchant_list'] as $kk=>$vv){
                    $shop_detail = Shop::get($v['shop_id']);

                    $bank_card_list = $auth_model->cardList($v['shop_id']);
                    $bank_card_list = $bank_card_list['bank_card_list'];
                    $bank_info = [];
                    foreach ($bank_card_list as $k=>$v){
                        if ($v['is_settle_card']){
                            $bank_info = $v;
                            /*$shop = Shop::get($v['shop_id']);
                            $shop->bank_name = $v['bank_name'];
                            $shop->bank_num = $v['card_no'];
                            $shop->bank = $v['bank_contact_name'];
                            $shop->bank_code = $v['bank_contact_no'];
                            $shop->bank_card_id = $v['card_id'];
                            $shop->save();*/
                        }
                    }
                    //$shop_detail = Shop::get($v['shop_id']);
                    $update_data = [
                        'merchant_no'=>$vv['merchant_no'],
                        'member_no'=>$vv['member_no'],
                        'audit_status'=>$vv['merchant_status'],
                        'out_merchant_no'=>$vv['out_merchant_no'],
                        'bank_name'=>$bank_info['bank_name'],
                        'bank_num'=>$bank_info['card_no'],
                        'bank'=>$bank_info['bank_contact_name'],
                        'bank_code'=>$bank_info['bank_contact_no'],
                        'bank_card_id'=>$bank_info['card_id'],
                    ];
                    //$wx_sub_merchant = $auth_model->searchMerchant($v['shop_id']);
                    //$update_data['wechat_channel_sub_merchant_no'] = $wx_sub_merchant['channel_sub_merchant_no'];
                    //$update_data = array_merge($update_data,$new_update_data);
                    //Shop::update($update_data,['shop_id'=>$v['shop_id']]);
                    $shop_detail->save($update_data);
                }
            }


        }


        $pass_shop_list = Shop::alias('s')
            //->join('user u','s.user_id = u.id')
            ->where('s.audit_status',3)
            ->field('s.out_member_no,s.shop_id')
            ->select();

        foreach ($pass_shop_list as $k=>$v){
            $shop_detail = Shop::get($v['shop_id']);
            $wx_sub_merchant = $auth_model->searchMerchant($v['shop_id']);
            $update_data['wechat_channel_sub_merchant_no'] = $wx_sub_merchant['channel_sub_merchant_no'];
            $shop_detail->save($update_data);
        }

    }
}